// server/app/model/inbound_order_item_model.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, DECIMAL } = app.Sequelize;

  const InboundOrderItemModel = app.model.define('inbound_order_item', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    inboundOrderId: {
      type: INTEGER,
      allowNull: false,
      field: 'inbound_order_id',
      comment: '入库单ID'
    },
    productId: {
      type: INTEGER,
      allowNull: true,
      field: 'product_id',
      comment: '商品ID'
    },
    productName: {
      type: STRING(100),
      allowNull: false,
      field: 'product_name',
      comment: '商品名称'
    },
    specification: {
      type: STRING(100),
      allowNull: true,
      field: 'specification',
      comment: '规格'
    },
    quantity: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 1,
      field: 'quantity',
      comment: '数量'
    },
    unitPrice: {
      type: DECIMAL(10, 2),
      allowNull: false,
      defaultValue: 0,
      field: 'unit_price',
      comment: '单价'
    },
    createdAt: {
      type: DATE,
      allowNull: false,
      defaultValue: new Date(),
      field: 'created_at',
      comment: '创建时间'
    },
    updatedAt: {
      type: DATE,
      allowNull: false,
      defaultValue: new Date(),
      field: 'updated_at',
      comment: '更新时间'
    }
  }, {
    tableName: 'inbound_order_items',
    timestamps: true,
    createdAt: 'createdAt',
    updatedAt: 'updatedAt'
  });

  // 关联关系
  InboundOrderItemModel.associate = function() {
    app.model.InboundOrderItemModel.belongsTo(app.model.InboundOrderModel, {
      as: 'inboundOrder',
      foreignKey: 'inbound_order_id'
    });
    
    app.model.InboundOrderItemModel.belongsTo(app.model.ProductModel, {
      as: 'product',
      foreignKey: 'product_id'
    });
  };

  return InboundOrderItemModel;
};